package com.config;

import com.config.globalException.BusinessException;
import org.apache.poi.POIXMLDocument;
import org.apache.poi.POIXMLTextExtractor;
import org.apache.poi.hwpf.extractor.WordExtractor;
import org.apache.poi.openxml4j.opc.OPCPackage;
import org.apache.poi.xwpf.extractor.XWPFWordExtractor;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;

public class DocTest {

    @Resource
    DocUtil docUtil;
    public static void main(String[] args) throws Exception {
        String doc = "C:\\Users\\12499\\Documents\\3_参考(1).doc";
        String word = readWord(doc);
        System.out.println(word);
        System.out.println("字数" + getMSWordsCount(word));
//        int[] ints = DocUtil.wordCountNew(doc, false);
//        for (int anInt : ints) {
//            System.out.println(anInt);
//        }
//        System.out.println(ints.toString());
    }

    public static String readWord(String path) {
        String buffer = "";
        try {
            if (path.endsWith(".doc")) {
                InputStream is = new FileInputStream(new File(path));
                WordExtractor ex = new WordExtractor(is);
                buffer = ex.getText();

            } else if (path.endsWith("docx")) {
                OPCPackage opcPackage = POIXMLDocument.openPackage(path);
                POIXMLTextExtractor extractor = new XWPFWordExtractor(opcPackage);
                buffer = extractor.getText();

            } else {
                throw new BusinessException("文件格式错误！");
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return buffer;
    }

    /**
     * 类似word统计字符数
     *
     * @param content
     * @return
     */
    public static int getMSWordsCount(String content) {
        //中文单词
//        content = content.replace("\n", "");
        String cnWords = content.replaceAll("[^(\\u4e00-\\u9fa5，。《》？；’‘：“”【】、）（……￥！·)]", "");
        int cnWordsCount = cnWords.length();
        //非中文单词
        String noCnWords = content.replaceAll("[^(a-zA-Z0-9`\\-=\';.,/~!@#$%^&*()_+|}{\":><?\\[\\])]", " ");
        int noCnWordsCount = 0;
        String[] ss = noCnWords.split(" ");
        for (String s : ss) {
            if (s.trim().length() != 0) {
                noCnWordsCount++;
            }
        }
        //中文和非中文单词合计
        return cnWordsCount + noCnWordsCount;
//
//        int words_count = 0;
//        //中文单词
//        String cn_words = context.replaceAll("[^(\\u4e00-\\u9fa5，。《》？；’‘：“”【】、）（……￥！·)]", "");
//        int cn_words_count = cn_words.length();
//        //非中文单词
//        String non_cn_words = context.replaceAll("[^(a-zA-Z0-9`\\-=\';.,/~!@#$%^&*()_+|}{\":><?\\[\\])]", " ");
//        int non_cn_words_count = 0;
//        String[] ss = non_cn_words.split(" ");
//        for (String s : ss) {
//            if (s.trim().length() != 0) non_cn_words_count++;
//        }
////中文和非中文单词合计
//        words_count = cn_words_count + non_cn_words_count;
////        ToolLog.d(ConstString.TAG, "汉字：" + cn_words_count + "\n\t字符：" + non_cn_words_count);
//        return words_count;
    }
}
